<template>
  <div class="loginWrap">
    <el-page-header @back="goBack" content="登录"> </el-page-header>
    <div class="logincontent">
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录" name="first" >
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="success" plain @click="toLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane  label="注册" name="second">
            <el-form
            :model="regForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="regForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="regForm.password"></el-input>
            </el-form-item>
             <el-form-item label="昵称" prop="nickname">
              <el-input v-model="regForm.nickname"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" plain @click="toReg">注册</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
    activeName: 'first',
      ruleForm: {
          username:'',
          password:''
      },
      regForm:{
          username:'',
          password:'',
          nickname:''
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
      ...mapActions({
      getLogin: "login/getLogin",
      getReg:"login/getReg"
    }),
    goBack() {
        console.log(1)
    //   this.$router.back();
    },
    toLogin(){
        console.log(this.ruleForm)
        this.getLogin(this.ruleForm)
        .then((res) => {
          this.$message({ message: res, type: "success" });
          this.$router.push('/home')
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    toReg(){
        console.log(this.regForm)
        this.getReg(this.regForm)
        .then((res)=>{
          this.$message({ message: res, type: "success" });
          this.activeName="first"
        })
        .catch((err)=>{
          this.$message.error(err);
        })
    }
  },
};
</script>

<style lang='scss'>
.loginWrap {
  width: 100%;
  height: 100%;
  .logincontent {
    width: 600px;
    height: 400px;
    background: #fff;
    border-radius: 20px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 20px 10px;
  }
  .el-form-item{
      padding-right: 60px;
  }
}
</style>